package info.marlan.sim;


import info.marlan.sim.util.DBHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class Simulator   extends Activity {

	
	private DBHelper dbHelper;
	public ProgressDialog pd;
	private SharedPreferences preferences;
	private Editor preferencesEditor;
    private boolean dialogComplete;
    
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        preferences =
        	Simulator.this.getSharedPreferences(
        	Constants.PREFERENCES, Context.MODE_PRIVATE);

        if("".equals(preferences.getString(Constants.PREFERENCES_LANGUAGE,""))){
        	Thread thread = new Thread(ImportDB);
        	       thread.start();
        	showDialog(0);
        }else{
        	dialogComplete = true; 
        	startApplication();
        }
    }
    
    
    Runnable ImportDB = new Runnable()
    {
        public void run()
        {
        	 dbHelper = new DBHelper(Simulator.this);
        	 handler.sendEmptyMessage(0);
        }
    }; 
	
	
	
    private Handler handler = new Handler() {
	        @Override
	        public void handleMessage(Message msg) {
	        if( pd != null ){
	           pd.dismiss();
	        }
	           startApplication();
	        }
    };
    
    private synchronized void startApplication(){
    	   if(dialogComplete){
        	   startActivity(new Intent(getApplication(),ObjectiveDialog.class));
        	   if(dbHelper != null){
        		   dbHelper.cleanup();
        	   }
        	   this.finish();
           }else{
        	   dialogComplete = true; 
           }
    }
    

    @Override
    protected Dialog onCreateDialog(int id) {

            return new AlertDialog.Builder(Simulator.this)
                .setTitle("Select Language")
                .setItems(getResources().getStringArray(R.array.languages), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String[] items = getResources().getStringArray(R.array.languagecodes);
                        preferencesEditor = Simulator.this.preferences.edit();
                        preferencesEditor.putString(Constants.PREFERENCES_LANGUAGE, items[which]);
                    	preferencesEditor.commit();  
                    	if( !dialogComplete ){
                    		pd = ProgressDialog.show(Simulator.this, Constants.PREFERENCES_LANGUAGE_CODE_DE.equals(items[which])?
                    				getString(R.string.wait_de):getString(R.string.wait_en)
                    				,Constants.PREFERENCES_LANGUAGE_CODE_DE.equals(items[which])?
                    						getString(R.string.importing_de):getString(R.string.importing_en)
                    						, true,false);
                    	}
                    	startApplication();
                    }
                })
                .create();
        }


	@Override
	protected void onPause() {
		super.onPause();
		if(dbHelper!=null){
			dbHelper.cleanup();
		}
	}



    
    
}